package com.molyfun.parents.activity.report.view;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.report.ReportFeedbackEditActivity;
import com.molyfun.parents.rest.bean.ReportFeedbackListResponse;
import com.molyfun.parents.utils.TimeUtils;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/13/16.
 */
public class ReportFeedbackView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.feedback_cell_avator)
    CircleImageView mFeedbackAvator;
    @InjectView(R.id.edit_feedback_btn)
    TextView mFeedbackBtn;
    @InjectView(R.id.feedback_author)
    TextView mFeedBackAuthor;
    @InjectView(R.id.feedback_content)
    TextView mFeedBackContent;
    @InjectView(R.id.feedback_time)
    TextView mFeedBackTime;

    ReportFeedbackListResponse.ReportFeedbackBean mReportFeedbackBean;
    private FragmentManager mFragmentManager;

    public ReportFeedbackView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ReportFeedbackView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ReportFeedbackView(Context context) {
        super(context);
        init(context, null);
    }


    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_report_feedback_cell, this);
        ButterKnife.inject(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edit_feedback_btn:

                ReportFeedbackEditActivity.setReportFeedbackCallback(new ReportFeedbackEditActivity.ReportFeedbackCallback() {
                    @Override
                    public void onFeedbackSuccess() {
                    }

                    @Override
                    public void onFeedbackEdtSuccess(String content) {
                        mFeedBackContent.setText(content);
                        mFeedBackTime.setText(TimeUtils.getQueryDate());
                    }
                });
                ReportFeedbackEditActivity.startActivity(context, mReportFeedbackBean.getReportid(), mReportFeedbackBean);

        }
    }

    /**
     * init data
     * @param reportFeedbackBean
     */
    public void initData(ReportFeedbackListResponse.ReportFeedbackBean reportFeedbackBean, FragmentManager fragmentManager) {

        if (reportFeedbackBean == null) {
            return;
        }

        mFragmentManager = fragmentManager;
        mReportFeedbackBean = reportFeedbackBean;
        mFeedBackAuthor.setText(reportFeedbackBean.getUsername());
        mFeedBackContent.setText(reportFeedbackBean.getContent());
        mFeedBackTime.setText(TimeUtils.transForDateTime(reportFeedbackBean.getCreatedatetime()));

        mFeedbackBtn.setOnClickListener(this);
        if(!TextUtils.isEmpty(mReportFeedbackBean.getUserimgurl())){
            Picasso.with(getContext())
                    .load(mReportFeedbackBean.getUserimgurl())
                    .into(mFeedbackAvator);
        }else{
            mFeedbackAvator.setImageResource(R.mipmap.ic_header_default);
        }
        if(mReportFeedbackBean.getUserid() != null && mReportFeedbackBean.getUserid().equals(MFPApplication.getInstance().getUser().getId())){
            mFeedbackBtn.setVisibility(View.VISIBLE);
        }else{
            mFeedbackBtn.setVisibility(View.GONE);
        }
    }

}